<template>
  <div class="ls-loading-mask" v-if="pageVisible" :style="{backgroundColor:maskColor}">
    <svg class="ls-loading-circular" viewBox="25 25 50 50">
      <circle class="ls-loading-path" cx="50" cy="50" r="20" fill="none" />
    </svg>
    <span class="ls-loading-text" :style="{color:textColor}">{{text}}</span>
  </div>
</template>

<script>
import { onMounted, ref, watch } from 'vue'
export default {
  name: 'LsLoading',
  props: {
    remove: {
      type: Function, //传入移除节点方法,这里是createApp中的方法
      default: null,
    },
    content: {
      type: String,
      default: '加载中...',
    },
    visible: {
      type: Boolean,
      default: false,
    },
    text: {
      type: String,
      default: '加载中...',
    },
    textColor: {
      type: String,
      default: '#fff',
    },
    maskColor: {
      type: String,
      default: 'rgba(0, 0, 0, 0.5)',
    },
  },
  setup(props) {
    const pageVisible = ref(false)
    pageVisible.value = props.visible

    onMounted(() => {
      document.body.style.overflow = 'hidden'
    })
    // 监听显示的消失，需要移除dom
    watch(
      () => pageVisible.value,
      (val) => {
        !val && props.remove()
      }
    )
    return {
      pageVisible,
    }
  },
}
</script>

<style src="../root.css"></style>
<style scoped>
@keyframes ls-loading-rotate {
  to {
    transform: rotate(360deg);
  }
}

@keyframes ls-loading-dash {
  0% {
    stroke-dasharray: 1, 200;
    stroke-dashoffset: 0;
  }

  50% {
    stroke-dasharray: 90, 150;
    stroke-dashoffset: -40px;
  }

  to {
    stroke-dasharray: 90, 150;
    stroke-dashoffset: -120px;
  }
}

.ls-loading-circular {
  display: inline;
  height: 50px;
  width: 50px;
  animation: ls-loading-rotate 2s linear infinite;
}

.ls-loading-path {
  animation: ls-loading-dash 1.5s ease-in-out infinite;
  stroke-dasharray: 90, 150;
  stroke-dashoffset: 0;
  stroke-width: 2;
  stroke: var(--primary);
  stroke-linecap: round;
}
.ls-loading-mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-flow: column;
}
</style>